import tqdm
import random


lazy_accum=0

def percent(num, total=0):
    if total == 0:
        return "{:.2f}%".format(num*100)
    return "{:.2f}%".format(num/total*100)

def lazy_print(text, interval,restore=False):
    global lazy_accum
    
    if restore:
        lazy_accum=0
        print(text)
    else:
        if lazy_accum>=interval:
            print(text)
            lazy_accum=0
        else:
            lazy_accum+=1
            
def lazy_rprint(text,prob=0.05):
    if random.random()<prob:
        print(text)

def printlist(lst):
    for e in lst:
        print(e)
        
def show_progress(iterable,length,call_back=None):
    for i,e in zip(tqdm.tqdm(length),iterable):
        if call_back:
            call_back(i,e)
            
def get_progress_bar(length):
    return tqdm.tqdm(length)